<?php

namespace App\Services\Wxtj;

use App\Facades\EasyWechat;
use App\Services\WxCommon\WxError;

/**
 * 用户分析数据服务类
 * @package App\Services
 */
class WxtjUser extends \App\Services\WxCommon\WxBase
{
    /**
     * 用户增减数据-用户的渠道
     * @var array
     */
    private $userSourceTypeList = [
        1 => '代表公众号搜索',
        17 => '代表名片分享',
        30 => '代表扫描二维码',
        43 => '代表图文页右上角菜单',
        51 => '代表支付后关注（在支付完成页）',
        57 => '代表图文页内公众号名称',
        75 => '代表公众号文章广告',
        78 => '代表朋友圈广告',
        0 => '其他合计',
    ];
    /**
     * 所有统计数据开始日期
     * @var string
     */
    private $allBeginDate;
    /**
     * 今天日期
     * @var false|string
     */
    private $todayDate;

    /**
     * 构造函数
     */
    public function __construct($appid = '')
    {
        parent::__construct($appid);
        //全部拉取开始日期
        $this->allBeginDate = '2014-12-01';
        //$this->allBeginDate = date('Y').'-01-01';
        $this->todayDate = date('Y-m-d');
    }

    /**
     * 用户的渠道
     * @return array
     */
    public function getUserSourceTypeList()
    {
        return $this->userSourceTypeList;
    }
    /**
     * 拉取所有用户增减数据
     * @return bool
     */
    public function pullAllSummary()
    {
        $beginDate = $this->allBeginDate;
        while ($beginDate < $this->todayDate){
            $endDate = date('Y-m-d', strtotime($beginDate.' +6 days'));
            if($endDate >= $this->todayDate ){
                $endDate = date('Y-m-d', strtotime($this->todayDate .' -1 days'));
            }
            //异步处理统计数据拉取
            \App\Jobs\WxtjUserSummaryHandle::dispatch($this->appid, $beginDate, $endDate)->onQueue('low');
            if($endDate >= $this->todayDate ){
                $beginDate = date('Y-m-d', strtotime($endDate . '+2 days'));
            }else{
                $beginDate = date('Y-m-d', strtotime($endDate. ' +1 days'));
            }
        };
        return true;
    }

    /**
     * 拉取指定日期范围内用户增减数据
     * @param string $beginDate
     * @param string $endDate
     * @return bool
     * @throws \Exception
     */
    public function pullSummary($beginDate='', $endDate='')
    {
        $beginDate || $beginDate = date('Y-m-d', strtotime('-7 days'));
        $endDate || $endDate = date('Y-m-d', strtotime('-1 days'));
        $app = EasyWechat::officialAccount($this->appid);
        $apiRs = $app->data_cube->userSummary($beginDate, $endDate);
        WxError::checkApiResponse($apiRs);
        if (!empty($apiRs['list'])){
            foreach ($apiRs['list'] as $v){
                \App\Models\WxtjUserSummary::updateOrCreate(
                    [
                        'appid' => $this->appid,
                        'ref_date' => $v['ref_date'],
                        'user_source' => $v['user_source']
                    ],
                    [
                        'new_user' => $v['new_user'],
                        'cancel_user' => $v['cancel_user'],
                    ]
                );
            }
        }
        return true;
    }

    /**
     * 拉取所有累计用户数据
     * @return bool
     */
    public function pullAllCumulate()
    {
        $beginDate = $this->allBeginDate;
        while ($beginDate < $this->todayDate){
            $endDate = date('Y-m-d', strtotime($beginDate.' +6 days'));
            if($endDate >= $this->todayDate ){
                $endDate = date('Y-m-d', strtotime($this->todayDate .' -1 days'));
            }
            //异步处理统计数据拉取
            \App\Jobs\WxtjUserCumulateHandle::dispatch($this->appid, $beginDate, $endDate)->onQueue('low');
            if($endDate >= $this->todayDate ){
                $beginDate = date('Y-m-d', strtotime($endDate . '+2 days'));
            }else{
                $beginDate = date('Y-m-d', strtotime($endDate. ' +1 days'));
            }
        };
        return true;
    }
    /**
     * 拉取指定日期范围内累计用户数据
     * @param string $beginDate
     * @param string $endDate
     * @return bool
     * @throws \Exception
     */
    public function pullCumulate($beginDate='', $endDate='')
    {
        $beginDate || $beginDate = date('Y-m-d', strtotime('-7 days'));
        $endDate || $endDate = date('Y-m-d', strtotime('-1 days'));
        $app = EasyWechat::officialAccount($this->appid);
        $apiRs = $app->data_cube->userCumulate($beginDate, $endDate);
        WxError::checkApiResponse($apiRs);
        if (!empty($apiRs['list'])){
            foreach ($apiRs['list'] as $v){
                \App\Models\WxtjUserCumulate::updateOrCreate(
                    [
                        'appid' => $this->appid,
                        'ref_date' => $v['ref_date'],
                    ],
                    [
                        'cumulate_user' => $v['cumulate_user'],
                    ]
                );
            }
        }
        return true;
    }
}